﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using LEEAAttendanceControl.Web;
using LEEAAttendanceControl.General;
using LEEAAttendanceControl.Web.Services;

using System.ServiceModel.DomainServices.Client;

namespace LEEAAttendanceControl.Views
{
    public partial class ShiftEdit : ChildWindow
    {
        LEEAContext context = new LEEAContext();

        public Shift currentShift = new Shift();

        public ShiftEdit()
        {
            InitializeComponent();
        }

        public ShiftEdit(string title)
        {
            Title = title;

            InitializeComponent();
        }

        public void LoadData(int id)
        {
            context.Load(context.GetShiftByIdQuery(id), OnShiftLoaded, null);
        }

        private void OnShiftLoaded(LoadOperation load)
        {
            context.Load(context.GetCompaniesQuery()).Completed += (s, e) =>
                {
                    CompanyIdBox.ItemsSource = context.Companies;

                    ColorBox.ItemsSource = NamedColor.GetColors();

                    currentShift = (Shift)load.Entities.FirstOrDefault();

                    if (currentShift == null)
                    {
                        currentShift = new Shift();

                        currentShift.WeeklyTime = 40;
                        currentShift.MinBreakDeduction = 5;

                        currentShift.FirstPeriodIN = 10;

                        currentShift.RountingTimeIN = 5;
                        currentShift.RountingTimeOUT = 5;

                        currentShift.GraceIN = 30;
                        currentShift.GraceOUT = 30;

                        context.Shifts.Add(currentShift);

                        context.GetShiftMaxId(id =>
                        {
                            ShiftIDBox.Text = ((id.Value != null) ? id.Value.ToString() : No.Zero.ToString());
                        }
                            , null);
                    }
                    else
                    {
                        ColorBox.SelectedValue = currentShift.Color;
                    }

                    DataContext = currentShift;

                    CompanyIdBox.SelectedValue = currentShift.CompanyID;
                };
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            context.SubmitChanges(OnChangesSubmitted, null);
        }

        private void OnChangesSubmitted(SubmitOperation so)
        {
            if (so.HasError)
            {
                so.MarkErrorAsHandled();
                MessageDisplay message = new MessageDisplay(ApplicationStrings.GeneralError
                    , ApplicationStrings.GeneralError, "Error");
                message.Show();
            }
            else
            {
                MessageDisplay message = new MessageDisplay(ApplicationStrings.GeneralSave
                    , ApplicationStrings.GeneralSave, "Save");

                message.Closed += (s, e) =>
                {
                    this.DialogResult = true;
                };

                message.Show();
            }
        }

        private void ColorBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            NamedColor color = ColorBox.SelectedItem as NamedColor;

            if (color != null)
            {
                currentShift.Color = color.ColorName;
                currentShift.ForeColor = color.ForeColor;
            }
        }
    }
}

